using AspDotNet.Core6.Mvc.ViewComponents.Models;
using AspDotNet.Core6.Mvc.ViewComponents.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace ViewComponentSample.ViewComponents
{
    public class ShoppingViewComponent : ViewComponent
    {
        ShoppingDbContext DbContext { get; set; }
        public ShoppingViewComponent(ShoppingDbContext dbContext)
        {
            this.DbContext = dbContext;
        }

        public async Task<IViewComponentResult> InvokeAsync(decimal maxPrice)
        {
            var filter = DbContext.Prodcuts.Where(p => p.Price < maxPrice)
                .ToListAsync();

            var products = await filter;
            string viewName = "default";
            if (maxPrice % 2 == 0)
            {
                viewName = "default1";
            }
            ViewBag.Products = products;
            return View(viewName, products);
        }
    }
}
